package com.beans;

import com.google.common.collect.Lists;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author FCHEN
 */
public class SysUser extends BaseBean {

    private Long userId;                                    // 用户编号
    private String username ;                               // 用户名
    private String password ;                               // 密码
    private String salt ;                                   // 盐值
    private Long deptId ;                                // 门店编号
    private String email ;                                  // 邮箱
    private String mobile ;                                 // 手机号
    private Integer status;	                                // 状态 0:禁用，1:正常
    private String bindMac ;                                // 绑定mac地址
    private String wechatNo ;                               // 微信号
    private String headPhoto ;                              // 头像
    private String userRole ;                               // 用户角色

    private String dName;
    private String rName;

    private List<SysMenu> menuList = Lists.newArrayList(); // 拥有菜单列表

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getBindMac() {
        return bindMac;
    }

    public void setBindMac(String bindMac) {
        this.bindMac = bindMac;
    }

    public String getWechatNo() {
        return wechatNo;
    }

    public void setWechatNo(String wechatNo) {
        this.wechatNo = wechatNo;
    }

    public String getHeadPhoto() {
        return headPhoto;
    }

    public void setHeadPhoto(String headPhoto) {
        this.headPhoto = headPhoto;
    }

    public String getUserRole() {
        return userRole;
    }

    public void setUserRole(String userRole) {
        this.userRole = userRole;
    }

    public List<SysMenu> getMenuList() {
        return menuList;
    }

    public void setMenuList(List<SysMenu> menuList) {
        this.menuList = menuList;
    }

    public List<Long> getMenuIdList() {
        List<Long> menuIdList = Lists.newArrayList();
        for (SysMenu menu : menuList) {
            menuIdList.add(menu.getMenuId());
        }
        return menuIdList;
    }

    public void setMenuIdList(List<Long> menuIdList) {
        menuList = Lists.newArrayList();
        for (Long menuId : menuIdList) {
            SysMenu menu = new SysMenu();
            menu.setMenuId(menuId);
            menuList.add(menu);
        }
    }

    public String getMenuIds() {
        return StringUtils.join(getMenuIdList(), ",");
    }

    public void setMenuIds(String menuIds) {
        menuList = Lists.newArrayList();
        if (menuIds != null){
            String[] ids = StringUtils.split(menuIds, ",");
            Long[] _ids = stringToLong(ids);
            setMenuIdList(Lists.newArrayList(_ids));
        }
    }

    /**
     * 获取权限字符串列表
     */
    public List<String> getPermissions() {
        List<String> permissions = Lists.newArrayList();
        for (SysMenu menu : menuList) {
            if (menu.getPermission()!=null && !"".equals(menu.getPermission())){
                permissions.add(menu.getPermission());
            }
        }
        return permissions;
    }

    public static Long[] stringToLong(String stringArray[]) {
        if (stringArray == null)
            return null;
        return (Long[]) ConvertUtils.convert(stringArray, Long.class);
    }

    public boolean isAdmin(){
        return isAdmin(this.userId);
    }

    public static boolean isAdmin(Long id){
        return id != null && 1L == id;
    }

    public String getdName() {
        return dName;
    }

    public void setdName(String dName) {
        this.dName = dName;
    }

    public String getrName() {
        return rName;
    }

    public void setrName(String rName) {
        this.rName = rName;
    }
   }
